package com.gitee.sidihuo.utils.util;

import com.alibaba.fastjson.JSONObject;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

import java.util.UUID;

public class JasyptUtil {
    /**
     * Jasypt生成加密结果
     *
     * @param password 配置文件中设定的加密密码 jasypt.encryptor.password
     * @param value    待加密值
     * @return
     */

    public static String encryptPwd(String password, String value) {

        PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();

        encryptOr.setConfig(cryptOr(password));

        String result = encryptOr.encrypt(value);

        return result;

    }


    /**
     * 解密
     *
     * @param password 配置文件中设定的加密密码 jasypt.encryptor.password
     * @param value    待解密密文
     * @return
     */

    public static String decyptPwd(String password, String value) {

        PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();

        encryptOr.setConfig(cryptOr(password));

        String result = encryptOr.decrypt(value);

        return result;

    }


    private static SimpleStringPBEConfig cryptOr(String password) {

        SimpleStringPBEConfig config = new SimpleStringPBEConfig();

        config.setPassword(password);

        config.setAlgorithm(StandardPBEByteEncryptor.DEFAULT_ALGORITHM);

        config.setKeyObtentionIterations("1000");

        config.setPoolSize("1");

        config.setProviderName("SunJCE");

        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");

        config.setStringOutputType("base64");

        return config;

    }

    public static String createAccessKey() {
        UUID uuid1 = UUID.randomUUID();
        String accessKey = uuid1.toString().replaceAll("-", "").substring(0, 5);
        String generatoredId15 = IdGenerator.generatorId15();
        accessKey = generatoredId15 + accessKey;
        return accessKey;
    }

    public static String createAccessSecret() {
        UUID uuid1 = UUID.randomUUID();
        String accessSecret = uuid1.toString().replaceAll("-", "");
        return accessSecret;
    }

    public static String randomToken() {
        UUID uuid1 = UUID.randomUUID();
        UUID uuid2 = UUID.randomUUID();
        String u1 = uuid1.toString().replaceAll("-", "");
        String u2 = uuid2.toString().replaceAll("-", "");
        return u1 + u2;
    }


    public static void main(String[] args) {
        System.out.println("#####");
        String accessKey = createAccessKey();
        String accessSecret = createAccessSecret();
        System.out.println("accessKey:" + accessKey);
        System.out.println("accessSecret:" + accessSecret);
        String systemId = "ai";
        String appId = "ai";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("accessKey", accessKey);
        jsonObject.put("p13", "admin");
        String secretValueOrigin = jsonObject.toJSONString();
        System.out.println("secretValueOrigin:" + secretValueOrigin);
        String encrypted = encryptPwd(accessSecret, secretValueOrigin);
        System.out.println("encrypted:" + encrypted);
        String decypted = decyptPwd(accessSecret, encrypted);
        System.out.println("decypted:" + decypted);
        System.out.println("#####");

//        #####
//        accessKey:240430090246749c5480
//        accessSecret:f255203f552d4b95af464ba87f749efc
//        secretValueOrigin:{"p13":"admin","accessKey":"240430090246749c5480"}
//        encrypted:KEWS1eBzKl4s8VrE9z9o3MYM4pC5lKAYUt53S51+6y5vfcGTloUFYIoy8HQqg6sQfU/s0ptKA3qgfLhzqmXh6Q==
//        decypted:{"p13":"admin","accessKey":"240430090246749c5480"}
//        #####

    }

}
